package com.yanhui.utils.sql;

import com.yanhui.utils.string._stringUtils;

public class _createSqlUtils {
	    
	    public static String executeCreateSql(String tableName, String tableComment, String[] fieldName, 
	    		String[] fieldType, String[] fieldLen, String[] fieldDefault, String[] fieldComment) throws Exception  
	    	{ 
	    	if(!_stringUtils.isNotNull(tableName)){
	    		return "";
	    	}
	    	String[] strs = new String[fieldName.length];
	    	for(int i = 0; i < fieldName.length; i++){
	    		String field = fieldName[i]+","+fieldType[i]+","+fieldLen[i]+","+fieldDefault[i]+","+fieldComment[i];
	    		strs[i] = field;
	    	}
	    	
	    	//字段名称，类型，长度（0为默认），默认值（-1为主键，注释）
//	        String[] strs = {"test_id,1,11,-1,id","test_name,2,255,1,姓名"};
	        String strs_sql="";
	        for(int i = 0; i < strs.length; i++){
	        	String[] strs_ = strs[i].split(",");
	        	String strs_sql_ = strs_[0] +" ";
	        	String strs_sql_type = switchType(strs_[1]);
	        	strs_sql_ += strs_sql_type + " ";
	        	if(!"".equals(strs_[2])){
	        		strs_sql_ += "("+strs_[2]+")" + " ";
	        	}else{
	        		if("2".equals(strs_[1])){
	        			strs_sql_ += "(255)" + " ";
	        		}
	        	}
	        	String strs_sql_deafault = switchDefaultType(strs_[3]);
	        	strs_sql_ += strs_sql_deafault + " ";
	        	strs_sql_ += "COMMENT '"+strs_[4]+"',";
	        	strs_sql += strs_sql_;
	        }
	        strs_sql = strs_sql.substring(0,strs_sql.length()-1);
	    	String executeSql = "CREATE TABLE "+tableName+" ("
        			+ strs_sql
        		+") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='"+tableComment+"';"; 
	    	return executeSql;
	    }  
	    
	    public static void main(String[] args) throws Exception  
	    {  
	        String tableName = "my_test05";
	        String tableNameComment = "基础信息";
	        //字段名称，类型，长度（0为默认），默认值（-1为主键，注释）
	        String[] strs = {"test_id,1,11,-1,id","test_name,2,255,1,姓名"};
	        String strs_sql="";
	        for(int i = 0; i < strs.length; i++){
	        	String[] strs_ = strs[i].split(",");
	        	String strs_sql_ = strs_[0] +" ";
	        	String strs_sql_type = switchType(strs_[1]);
	        	strs_sql_ += strs_sql_type + " ";
	        	strs_sql_ += "("+strs_[2]+")" + " ";
	        	String strs_sql_deafault = switchDefaultType(strs_[3]);
	        	strs_sql_ += strs_sql_deafault + " ";
	        	strs_sql_ += "COMMENT '"+strs_[4]+"',";
	        	strs_sql += strs_sql_;
	        }
	        strs_sql = strs_sql.substring(0,strs_sql.length()-1);
	        String executeSql = "CREATE TABLE "+tableName+" ("
	        			+ strs_sql
	        		+") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='"+tableNameComment+"'"; 
	        System.out.println(executeSql);
	    }
	    
	    /**
	     * 返回类型   1int 2varchar 3double 4datetime 5longtext
	     * @param type
	     * @return
	     */
		private static String switchType(String type) {
			String result = "";
			int type_ = Integer.parseInt(type);
			switch (type_) {
			case 1:
				result = "int";
				break;
			case 2:
				result = "varchar";
				break;
			case 3:
				result = "double";
				break;
			case 4:
				result = "datetime";
				break;
			case 5:
				result = "longtext";
				break;

			default:
				break;
			}
			return result;
		}  
		
		private static String switchDefaultType(String type) {
			String result = "";
			int type_ = Integer.parseInt(type);
			switch (type_) {
			case -1:
				result = "auto_increment PRIMARY KEY";
				break;
			case 0:
				result = "";
				break;
				
			default:
				result = "default '"+type+"'";
				break;
			}
			return result;
		}
		
	}  
